package com.mingqijia.gassafety.job.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.db.entity.AicallTask;
import com.mingqijia.gassafety.db.mapper.AicallTaskMapper;
import com.mingqijia.gassafety.job.dto.CallTaskDTO;
import com.mingqijia.gassafety.job.service.AicallTaskService;
import com.mingqijia.gassafety.shared.constant.CallTypeEnum;
import com.mingqijia.gassafety.shared.constant.GssBusinessNoticeConstant;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: duanhongxiang
 * @Date: 2024/1/24 15:18
 */
@Service
@Slf4j
public class AicallTaskServiceImpl extends ServiceImpl<AicallTaskMapper, AicallTask> implements AicallTaskService {
    @Value(value = "${gassafety.xf.call.appId}")
    public Integer appId;
    
    @Override
    public AicallTask getByParamCode(String paramCode, String spId) {
        Map<String, Object> cond = new HashMap<>();
        cond.put("paramCode", paramCode);
        cond.put("spId", spId);
        List<AicallTask> aicallTasks = baseMapper.findAiCallTask(cond);
        if (null == aicallTasks || aicallTasks.size() == 0) {
            return null;
        }
        return aicallTasks.get(0);
    }

    @Override
    public CallTaskDTO getAicallTaskByParamCode(String paramCode, String spId) {
        CallTaskDTO callTaskDTO = new CallTaskDTO();
        AicallTask aicallTask = this.getByParamCode(paramCode, spId);
        String taskCode = GssBusinessNoticeConstant.AICALL_PARAM_CODE_TASK_CODE_MAP.get(paramCode);
        log.info("外呼配置taskCode{}", taskCode);
        if (StringUtils.isEmpty(taskCode)) {
            return null;
        }
        CallTypeEnum callTypeEnum = CallTypeEnum.getPushTypesByTaskCode(taskCode);
        if (null == callTypeEnum) {
            log.error("外呼配置为空");
            return null;
        }
        log.info("外呼配置继续");
        if (null == aicallTask) {
            if( 99 == callTypeEnum.getCode() ){ //外呼机器人特殊规则，需要取apollo配置的appId
                callTaskDTO.setAppId(appId);
            }else{
                callTaskDTO.setAppId(callTypeEnum.getAppId());
            }
            callTaskDTO.setTaskCode(callTypeEnum.getTaskCode());
            callTaskDTO.setRobotContent("");
        }else{
            callTaskDTO.setAppId(aicallTask.getAppId());
            callTaskDTO.setTaskCode(aicallTask.getTaskCode());
            callTaskDTO.setRobotContent(StringUtils.isEmpty(aicallTask.getRobotContent())?"":aicallTask.getRobotContent());
        }
        callTaskDTO.setCallColumn(JSONArray.parseArray(callTypeEnum.getCallColumn()));
        log.info("外呼配置{}", JSON.toJSONString(callTaskDTO));
        return callTaskDTO;
    }

    @Override
    public CallTaskDTO getAicallTaskByType(Integer type, String spId) {
        String paramCode = "";
        log.info("外呼配置getAicallTaskByType：{}",type);
        if (null == type) {//null 机器人报警
            paramCode = GssBusinessNoticeConstant.OUTBOUND_CUSTOM_ALERT_PARAM_CODE;
        }else {
            switch (type) {
                case 0: //设备离线
                    paramCode = GssBusinessNoticeConstant.OUTBOUND_CUSTOM_OFFLINE_PARAM_CODE;
                    break;
                case 1: //设备故障
                    paramCode = GssBusinessNoticeConstant.OUTBOUND_CUSTOM_BROKEN_PARAM_CODE;
                    break;
                case 2: //设备运行报告
                    paramCode = GssBusinessNoticeConstant.OUTBOUND_CUSTOM_RUN_PARAM_CODE;
                    break;
                case 3: // 服务到期
                    paramCode = GssBusinessNoticeConstant.OUTBOUND_CUSTOM_SERVICE_PARAM_CODE;
                    break;
                default:
            }
        }
        log.info("外呼配置paramCode：{}",paramCode);
        if( StringUtils.isEmpty(paramCode) ){
            return null;
        }
        return this.getAicallTaskByParamCode(paramCode, spId);
    }
    
}
